package com.ikangtai.shecare.common.db.sync;

import android.content.Context;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.db.DBManager;
import com.ikangtai.shecare.common.eventBusMsg.sync.SyncHealthInfoMsg;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.OKHttpUtil;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;
import org.simple.eventbus.EventBus;

import java.io.IOException;

public class SyncHealthInfo {
    private Context context;
    private SyncHealthInfoMsg syncHealthInfoMsg;

    public SyncHealthInfo(Context context) {
        this.context = context;
    }

    public SyncHealthInfo(Context context, SyncHealthInfoMsg syncHealthInfoMsg) {
        this.context = context;
        this.syncHealthInfoMsg = syncHealthInfoMsg;
        EventBus.getDefault().register(this);
    }

    public void syncHealthInfoWithNetwork(){
        final DBManager dbManager = new DBManager(context);

        HealthInfo healthInfo = dbManager.getUnSyncedHealthInfo(App.userName);

        if (healthInfo == null){
            return;
        } else {
            try {
                OKHttpUtil okHttpClient = new OKHttpUtil(context);
                String appendUrl = "user/registerUserInfo.json";

                AppLog.i("syncHealthInfoWithNetwork is syncing ...!");

                okHttpClient.add("authToken", App.authToken);
                okHttpClient.add("status", App.status);
                okHttpClient.add("birthday", healthInfo.getBirthday());
                okHttpClient.add("mensesLen", healthInfo.getMensesLen());
                okHttpClient.add("periodLen", healthInfo.getPeriodLen());
                okHttpClient.add("mensesType", healthInfo.getMensesType());
                okHttpClient.add("lastPeriodDate", healthInfo.getLastPeriodDate());

                okHttpClient.postAsync(appendUrl, okHttpClient.new BaseCallBack() {

                    @Override
                    public void onResponse(Response response) throws IOException {
                        int respCode = 0;

                        try {
                            JSONObject respJSONObj = new JSONObject(response.body().string());
                            respCode = respJSONObj.getInt("code");

                            if (App.HTTP_RESP_OK == respCode){
                                dbManager.updateSyncHealthInfoFlag(App.userName);
                                AppLog.i("syncHealthInfoWithNetwork onResponse success!");
                            } else if (respCode == App.HTTP_RESP_TOKEN_TIME_OUT) {
                                OKHttpUtil.setNewToken();
                                syncHealthInfoWithNetwork();
                            } else {
                                AppLog.i("syncHealthInfoWithNetwork onResponse failed! respCode is:" + respCode);
                            }
                        } catch (JSONException e) {
                            AppLog.i("syncHealthInfoWithNetwork JSONException!");
                            e.printStackTrace();
                        }
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
